var e = require("../../@babel/runtime/helpers/interopRequireDefault"), t = require("../../@babel/runtime/helpers/toConsumableArray"), a = e(require("../../@babel/runtime/regenerator")), r = require("../../@babel/runtime/helpers/asyncToGenerator"), n = e(require("../../behavior/indexBehavior")), o = require("../../utils/switchTab"), i = require("../../utils/tool"), s = e(require("../../http/robot")), u = getApp(), c = (require("../../utils/api.js"), 
require("../../http/indexApi.js")), l = require("../../http/activityApi.js"), p = require("../../http/productApi.js"), d = require("../../utils/timeUtil.js"), f = new s.default();

Page({
    behaviors: [ n.default ],
    data: {
        systemInfo: u.data.systemInfo,
        menuType: 1,
        ipTopBgUrl: "http://newimg.ap1983.com/prod/20210730/83cd6aacaf0b432bb38cfab14100d60e.jpg",
        productList: [],
        hasMore: !0,
        showBarrage: !1
    },
    onLoad: function(e) {
        this.loadBasicData(), this.loadVipChangeInfo(), this.loadNewRecommendList(), this.loadProductList(), 
        this.loadNewUserGift(), e.fromSms && (this.fromSms = e.fromSms), e.loginAuthCode && !u.isLogin() && wx.navigateTo({
            url: "/pages/login/login?loginAuthCode=".concat(e.loginAuthCode)
        }), e.loginAuthCode && u.isLogin() && this.robotLogin(e.loginAuthCode);
    },
    testScan: function() {
        u.appScan();
    },
    onShow: function() {
        var e = this;
        this.startSuperCouponTimer(), (0, o.switchToTab)(this, 0), u.globalData.callback = function(t) {
            "BRIGHT_CLAASSIFY_CHAGER" != t.businessType && "BRIGHT_CLAASSIFY_OUT" != t.businessType || e.refreshLineInfo();
        }, this.refreshLineInfo(1e3), this.loadBarrage(), u.data.offlinePaySuccess > 0 && (this.getTabBar().showOfflinePaySuccess(u.data.offlinePaySuccess), 
        u.data.offlinePaySuccess = 0);
    },
    onHide: function() {
        this.stopSuperCouponTimer(), this.clearBarrage();
    },
    onUnload: function() {
        this.stopSuperCouponTimer(), this.clearBarrage();
    },
    startSuperCouponTimer: function() {
        var e = this;
        if (!this.data.showSuperCouponResult) {
            var t = Math.round(5 * Math.random() + 3);
            this.stopSuperCouponTimer(), this.superCouponTimer = setTimeout(function() {
                e.getSuperCoupon();
            }, 1e3 * t);
        }
    },
    stopSuperCouponTimer: function() {
        this.superCouponTimer && (clearTimeout(this.superCouponTimer), this.superCouponTimer = null);
    },
    superCouponResOnClose: function() {},
    getSuperCoupon: function() {
        var e = this;
        return r(a.default.mark(function t() {
            var r, n, o;
            return a.default.wrap(function(t) {
                for (;;) switch (t.prev = t.next) {
                  case 0:
                    if (u.isLogin()) {
                        t.next = 2;
                        break;
                    }
                    return t.abrupt("return");

                  case 2:
                    return t.prev = 2, t.next = 5, l.superCouponGet({
                        shareFrom: "",
                        usedId: ""
                    });

                  case 5:
                    if (!(r = t.sent).data) {
                        t.next = 14;
                        break;
                    }
                    return l.superCouponHomeTag({
                        activityId: r.data.activityId
                    }).then(function(e) {}).catch(function(e) {}), t.next = 10, l.getServerTime();

                  case 10:
                    n = t.sent, o = n.data.systemTime - new Date().getTime(), r.data.localEndTime = d.timeStringToLong(r.data.usedCloseTime) - o, 
                    e.setData({
                        showSuperCouponResult: !0,
                        superCouponResult: r.data
                    });

                  case 14:
                    t.next = 19;
                    break;

                  case 16:
                    t.prev = 16, t.t0 = t.catch(2), console.log(t.t0);

                  case 19:
                  case "end":
                    return t.stop();
                }
            }, t, null, [ [ 2, 16 ] ]);
        }))();
    },
    nothing: function() {},
    clearBarrage: function() {
        this.setData({
            showBarrage: !1
        }), this.timer && clearInterval(this.timer), this.barrage && this.barrage.close();
    },
    robotLogin: function(e) {
        return r(a.default.mark(function t() {
            return a.default.wrap(function(t) {
                for (;;) switch (t.prev = t.next) {
                  case 0:
                    return t.prev = 0, t.next = 3, f.robotLogin({
                        loginAuthCode: e,
                        memberId: u.data.loginInfo.memberId
                    });

                  case 3:
                    t.sent, (0, i.toastToPage)("机器登陆成功"), t.next = 9;
                    break;

                  case 7:
                    t.prev = 7, t.t0 = t.catch(0);

                  case 9:
                  case "end":
                    return t.stop();
                }
            }, t, null, [ [ 0, 7 ] ]);
        }))();
    },
    onTabItemTap: function(e) {
        console.log("onTabItemTap:", e);
    },
    menuItemOnTap: function(e) {
        var t = e.currentTarget.dataset.type;
        t != this.data.menuType && (this.setData({
            menuType: t,
            whiteTheme: !1,
            scrollTopY: 0
        }), 1 == t ? this.loadBarrage() : this.clearBarrage());
    },
    bannerOnTap: function(e) {
        var t = e.currentTarget.dataset.data;
        u.bannerOnTap(t);
    },
    activityItemOnTap: function(e) {
        var t = e.currentTarget.dataset.data;
        u.bannerOnTap(t);
    },
    ipOnTap: function(e) {
        var t = e.currentTarget.dataset.data;
        wx.navigateTo({
            url: "/pages/ip-product-list/ip-product-list?ipId=" + t.ipId
        });
    },
    lineTipsOnTap: function() {
        var e = this.data.myLineInfo.productId, t = this.data.myLineInfo.memberQueueId;
        wx.navigateTo({
            url: "/pages/series/series?productId=" + e + "&queueId=" + t
        });
    },
    closeAdOnTap: function() {
        this.setData({
            adInfo: null
        });
    },
    scrollOnTop: function() {},
    onScroll: function(e) {
        var t = u.data.systemInfo.windowWidth, a = 750 * u.data.systemInfo.navBarHeight / t + 100, r = 0;
        r = 1 == this.data.menuType ? 570 - a : 400 - a;
        var n = !1;
        e.detail.scrollTop > t / 750 * r && (n = !0), this.data.whiteTheme != n && this.setData({
            whiteTheme: n
        });
    },
    tuijianScrollOnBottom: function() {
        this.loadProductList();
    },
    loadBasicData: function() {
        var e = this;
        return r(a.default.mark(function t() {
            var r;
            return a.default.wrap(function(t) {
                for (;;) switch (t.prev = t.next) {
                  case 0:
                    return u.showLoading(), t.prev = 1, t.next = 4, c.home();

                  case 4:
                    r = t.sent, e.setData({
                        adInfo: r.data.dialogList[0] || null,
                        bannerList: r.data.bannerList,
                        activityList: r.data.activityList,
                        ipList: r.data.ipList
                    }), t.next = 11;
                    break;

                  case 8:
                    t.prev = 8, t.t0 = t.catch(1), console.log("loadBasicData:", t.t0);

                  case 11:
                    return t.prev = 11, wx.hideLoading(), t.finish(11);

                  case 14:
                  case "end":
                    return t.stop();
                }
            }, t, null, [ [ 1, 8, 11, 14 ] ]);
        }))();
    },
    loadNewRecommendList: function() {
        var e = this;
        return r(a.default.mark(function t() {
            var r;
            return a.default.wrap(function(t) {
                for (;;) switch (t.prev = t.next) {
                  case 0:
                    return t.prev = 0, t.next = 3, c.newRecommendList();

                  case 3:
                    r = t.sent, e.setData({
                        newRecommendList: r.data
                    }), t.next = 10;
                    break;

                  case 7:
                    t.prev = 7, t.t0 = t.catch(0), console.log("loadNewRecommendList:", t.t0);

                  case 10:
                  case "end":
                    return t.stop();
                }
            }, t, null, [ [ 0, 7 ] ]);
        }))();
    },
    loadProductList: function() {
        var e = this;
        return r(a.default.mark(function r() {
            var n, o, i, s;
            return a.default.wrap(function(a) {
                for (;;) switch (a.prev = a.next) {
                  case 0:
                    if (e.data.hasMore && !e.data.loadingMore) {
                        a.next = 2;
                        break;
                    }
                    return a.abrupt("return");

                  case 2:
                    return 20, n = e.data.productList.length / 20 + 1, e.setData({
                        loadingMore: !0
                    }), a.prev = 5, a.next = 8, p.productList(n, 20);

                  case 8:
                    o = a.sent, i = [].concat(t(e.data.productList), t(o.data.list)), e.setData({
                        productList: i,
                        hasMore: o.data.totalPage > o.data.currPage
                    }), s = e.selectComponent("#waterFallView"), 1 == o.data.currPage ? s.initData(null, o.data.list) : s.appendProducts(o.data.list), 
                    a.next = 18;
                    break;

                  case 15:
                    a.prev = 15, a.t0 = a.catch(5), console.log("loadProductList:", a.t0);

                  case 18:
                    return a.prev = 18, e.setData({
                        loadingMore: !1
                    }), a.finish(18);

                  case 21:
                  case "end":
                    return a.stop();
                }
            }, r, null, [ [ 5, 15, 18, 21 ] ]);
        }))();
    },
    loadVipChangeInfo: function() {
        var e = this;
        return r(a.default.mark(function t() {
            var r, n;
            return a.default.wrap(function(t) {
                for (;;) switch (t.prev = t.next) {
                  case 0:
                    if (u.isLogin()) {
                        t.next = 3;
                        break;
                    }
                    return console.log("会员定级：未登录，不加载"), t.abrupt("return");

                  case 3:
                    return t.prev = 3, console.log("会员定级：加载中…"), t.next = 7, c.getVipChangeInfo();

                  case 7:
                    r = t.sent, console.log("会员定级：结果：", r), (n = r.data) && 1 == n.newVipLevel && (n.couponId || n.tipCardNum || n.displayCardNum || (n = null)), 
                    e.setData({
                        vipChangeInfo: n
                    }), t.next = 17;
                    break;

                  case 14:
                    t.prev = 14, t.t0 = t.catch(3), console.log("会员定级：异常：", t.t0);

                  case 17:
                  case "end":
                    return t.stop();
                }
            }, t, null, [ [ 3, 14 ] ]);
        }))();
    },
    loadNewUserGift: function() {
        var e = this;
        return r(a.default.mark(function t() {
            var r;
            return a.default.wrap(function(t) {
                for (;;) switch (t.prev = t.next) {
                  case 0:
                    if (u.isLogin()) {
                        t.next = 11;
                        break;
                    }
                    return t.prev = 1, t.next = 4, l.newUserGift();

                  case 4:
                    r = t.sent, e.setData({
                        newUserGift: r.data
                    }), t.next = 11;
                    break;

                  case 8:
                    t.prev = 8, t.t0 = t.catch(1), console.log("loadNewUserGift:", t.t0);

                  case 11:
                  case "end":
                    return t.stop();
                }
            }, t, null, [ [ 1, 8 ] ]);
        }))();
    },
    loadBarrage: function() {
        var e = this;
        return r(a.default.mark(function t() {
            var r, n, o, i, s, u, l, p, d;
            return a.default.wrap(function(t) {
                for (;;) switch (t.prev = t.next) {
                  case 0:
                    if (e.timer && clearInterval(e.timer), !1 !== wx.getStorageSync("BarrageToggle")) {
                        t.next = 3;
                        break;
                    }
                    return t.abrupt("return");

                  case 3:
                    return t.prev = 3, t.next = 6, c.getHomeBrightBarrage();

                  case 6:
                    if (r = t.sent, (n = r.data.list) && 0 != n.length) {
                        t.next = 10;
                        break;
                    }
                    return t.abrupt("return");

                  case 10:
                    for (e.setData({
                        showBarrage: !0
                    }), o = [], i = 0; i < n.length; i++) (s = n[i]).nickName = s.nickName.substring(0, 20), 
                    s.nickName = s.nickName.replace("\n", ""), u = "rgba(0, 0, 0, 0.7)", 0 != s.skuType && (u = "rgba(200, 160, 0, 0.9)"), 
                    o.push({
                        content: "恭喜".concat(s.nickName, "抽到了").concat(s.skuTitle),
                        color: "#fff",
                        bgColor: u,
                        head: s.avatar
                    });
                    l = e.selectComponent(".barrage"), e.barrage = l.getBarrageInstance({
                        font: "12px sans-serif",
                        duration: 20,
                        lineHeight: 2.7
                    }), e.barrage.open(), p = 0, d = o.length, e.timer = setInterval(function() {
                        var t = [ o[p] ];
                        e.barrage.addData(t), ++p == d && (p = 0);
                    }, 1e3), t.next = 24;
                    break;

                  case 21:
                    t.prev = 21, t.t0 = t.catch(3), console.log("loadBarrage:", t.t0);

                  case 24:
                  case "end":
                    return t.stop();
                }
            }, t, null, [ [ 3, 21 ] ]);
        }))();
    },
    refreshLineInfo: function() {
        var e = arguments, t = this;
        return r(a.default.mark(function r() {
            var n, o;
            return a.default.wrap(function(a) {
                for (;;) switch (a.prev = a.next) {
                  case 0:
                    if (!((n = e.length > 0 && void 0 !== e[0] ? e[0] : 0) > 0)) {
                        a.next = 4;
                        break;
                    }
                    return setTimeout(function() {
                        t.refreshLineInfo();
                    }, n), a.abrupt("return");

                  case 4:
                    if (!u.isLogin()) {
                        a.next = 16;
                        break;
                    }
                    return a.prev = 5, a.next = 8, p.myLineInfo();

                  case 8:
                    o = a.sent, t.setData({
                        myLineInfo: o.data || null
                    }), u.data.myLineInfo = t.data.myLineInfo, a.next = 16;
                    break;

                  case 13:
                    a.prev = 13, a.t0 = a.catch(5), console.log("refreshLineInfo:", a.t0);

                  case 16:
                  case "end":
                    return a.stop();
                }
            }, r, null, [ [ 5, 13 ] ]);
        }))();
    }
});